Repository navigation
Make MP4 decoding on Windows start and end where Core Audio does - #83
ni-eeviston wants to merge 9 commits into
Conversation
AAC files begin with a short stretch of encoder "priming" that decoders must skip. When opening an MP4 file, media_foundation_source jumped past it to the start of the real audio. But Windows Media Foundation fades in the first audio it decodes after any jump, so the first milliseconds of every AAC file came out faded. Seeking near the start had the same effect. Decode from the beginning of the file instead. Any fade-in now falls on the priming, which is thrown away. Also fix seekInternal remembering the first file's priming length for the rest of the session (it was stored in a static variable). This was harmless while every MP4 file was treated as having the same priming length, but would break once it is read from each file.
fbd4ea7 to
519448b
Compare
|
@marcrambo @FalconPDX @ni-mheppner can you add review this please? i can't seem to add reviewers. i will send the link for this PR to my Synths & FX team as well. |
519448b to
329c1d2
Compare
After a seek, media_foundation_source discards the first two decoded blocks, because Media Foundation fades them in. It jumps two blocks of 1024 frames before the target to make room for them. ALAC decodes 4096 frames per block and MP3 about 15000, so a discarded block could already contain the target. Near the end of the file nothing followed it, and the seek returned silence. Jump back two of the largest blocks decoded so far instead. When a discarded block still reaches the target, seek further back and try again, as is already done when a seek lands past the target, doubling the step each time so a seek takes only a few tries.
AAC files begin with a short stretch of encoder "priming" that decoders
must skip, and often end with some "padding". media_foundation_source
skipped a fixed 2112 frames at the start of every MP4 file, the priming
of Apple's AAC encoder, and kept the padding. Files with any other
priming started at a different sample than on macOS, and ALAC files,
which have no priming, lost their first 2112 frames.
Files declare their priming in an iTunSMPB tag, as iTunes writes, or
in the edit list, as ffmpeg and Apple's MP4 writer do. Measured with
Core Audio on macOS, the declaration it follows is:
- the edit list, if the sample table has a roll sample group, as newer
encoders write;
- otherwise the iTunSMPB tag, if 'M4A ' or 'M4B ' appears among the
file's brands;
- otherwise, for AAC, the standard 2112 frames, with nothing trimmed
from the end, which counts the last packet in full.
Do the same. Media Foundation reports the iTunSMPB values, but the
edit list, roll sample group, brands and packet counts are read from
the file.
Media Foundation takes its duration from the movie header, which
excludes the priming and padding when the file has an edit list. So
the length is taken from the media header when the iTunSMPB tag
decides, and from the packet count when the standard priming is
assumed.
Media Foundation on Windows 10 timestamps the first decoded frame
with the start of the edit list, while Windows 11 timestamps it with
0; both decode the same frames. The timestamp of the first frame is
read when the file is opened and taken as the origin of all
timestamps and seek positions.
The file's sound tracks are only used when they match the audio
streams Media Foundation reports. Otherwise, AAC without an iTunSMPB
tag keeps the standard 2112 frames. An edit list that starts past the
end of the media is malformed and ignored, and parsing stops at a box
that doesn't fit in the file.
Test files, each a 1 second 440 Hz sine:
- sin440.1.44100.44100.s16le.aac192.ffmpeg.m4a, a reference file whose
priming of 1024 frames is declared by an edit list, made with ffmpeg
(libavformat 63.21.100):
ffmpeg -f lavfi -i "sine=frequency=440:sample_rate=44100:duration=1" \
-ac 1 -c:a aac -b:a 192k sin440.1.44100.44100.s16le.aac192.ffmpeg.m4a
- sin440.ffmpeg.*.m4a: that file with its edit list set to play all of
the media, its roll sample group removed, or both;
- sin440.apple.*.m4a: a file encoded by afconvert, whose encoder primed
2112 frames, with its edit list, iTunSMPB tag, roll sample group and
brands changed to each case the rule distinguishes.
The expected results were measured with Core Audio on macOS.
The MP4 decoding tests only ran on macOS, so Windows MP4 decoding was never tested. Run them on Windows too, and add a test that seeking returns the same audio as reading from the start, as media_foundation_source discards the encoder priming and the blocks Media Foundation fades in after a seek.
329c1d2 to
51bd894
Compare
|
CI: Windows 2022 and Ubuntu 22.04 pass. The macOS, Ubuntu 20.04 and Code-Coverage jobs never start on any PR because they target paid or retired runners. macOS was tested locally on Apple Silicon (M1) and in a fork CI run on macos-15-intel. I think in order to make them run, |
|
would be great to update the runner images - here's a list of what they have available: https://github.com/actions/runner-images |
Thanks for the pointer. I can do that some time this week. @ni-eeviston Is it ok for you, if I just add the change on top of your branch, or would prefer to have this separately integrated before your work lands? |
sure, do whatever is easiest for you |
On arm64 Linux system the assumption of 'char' being unsigned does not hold.
…errors caused by macros
On Windows, MP4/M4A audio decoded through Media Foundation started and ended at different samples than on macOS (Core Audio), and its first milliseconds were faded in. ALAC files lost their first 2112 frames, and seeks near the end of ALAC files could return silence. This makes the Windows decoder match Core Audio.
Each commit is a self-contained change so reviewing by commit may be easiest:
(fixes seeks near the end of ALAC files returning silence)
(the edit list, iTunSMPB tag or standard 2112 frames, chosen as Core Audio
chooses them)
Impact
sample as on macOS (shifts of up to 2112 frames, ~48 ms at 44.1 kHz) and no
longer fades in. Sample positions stored for M4A files analysed on Windows,
such as Traktor cue points and beat grids, may shift; they will now match
macOS. Are there other products using this path whose owners should know?
Testing
whose expected results were measured with Core Audio on macOS.
(afconvert, avconvert, AVAssetWriter; AAC-LC, HE-AAC, ALAC; .m4a and .mp4) and
a hand-made matrix of brand x roll group x edit list x iTunSMPB combinations.
95 decode identically on Windows; the rest are listed below.
Known differences from Core Audio (not addressed here)
rate), so length and alignment differ. This already happened before this PR.
Windows the start is off by 1024 frames. Rare in audio files.
decodes nothing; this PR ignores the edit list and decodes the file.
Context: MASSIVEX-4484, where M4A wavetable imports started at a different point on Windows than on macOS. Its unit tests pass with this change.